package com.hytc.mymall.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Camera;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.hytc.mymall.R;
import com.nineoldandroids.view.ViewHelper;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2016/12/2.
 */
public class JazzyViewPager extends ViewPager
{
    private static final boolean API_11 = false;
    private static final float SCALE_MAX = 0.5f;
    public static int sOutlineColor;
    public int currentPostion;
    public Handler handler;
    private Camera mCamera;
    private boolean mEnabled;
    private boolean mFadeEnabled;
    private View mLeft;
    private Matrix mMatrix;
    private HashMap<Integer, Object> mObjs;
    private boolean mOutlineEnabled;
    private View mRight;
    private float mRot;
    private float mScale;
    private State mState;
    private float[] mTempFloat2;
    private float mTrans;
    private int oldPage;
    private TaskRunnable runnable;

    static {
        JazzyViewPager.sOutlineColor = -1;
    }

    public JazzyViewPager(final Context context) {
        this(context, null);
    }

    public JazzyViewPager(final Context context, final AttributeSet set) {
        super(context, set);
        this.mEnabled = true;
        this.mFadeEnabled = false;
        this.mOutlineEnabled = false;
        this.mObjs = new LinkedHashMap<Integer, Object>();
        this.mMatrix = new Matrix();
        this.mCamera = new Camera();
        this.mTempFloat2 = new float[2];
        this.handler = new Handler() {
            public void handleMessage(final Message message) {
                JazzyViewPager.this.setCurrentItem(JazzyViewPager.this.currentPostion);
                JazzyViewPager.this.startRoll();
            }
        };
        this.setClipChildren(false);
        final TypedArray obtainStyledAttributes = context.obtainStyledAttributes(set, R.styleable.JazzyViewPager);
        this.setFadeEnabled(obtainStyledAttributes.getBoolean(R.styleable.JazzyViewPager_fadeEnabled, false));
        this.setOutlineEnabled(obtainStyledAttributes.getBoolean(R.styleable.JazzyViewPager_outlineEnabled, false));
        this.setOutlineColor(obtainStyledAttributes.getColor(R.styleable.JazzyViewPager_outlineColor, Color.WHITE));
        this.runnable = new TaskRunnable();
        obtainStyledAttributes.recycle();
    }

    private void animateCube(final View view, final View view2, final float n, final boolean b) {
        float n2 = 90.0f;
        if (this.mState != State.IDLE) {
            if (view != null) {
                this.manageLayer(view, true);
                float n3;
                if (b) {
                    n3 = n2;
                }
                else {
                    n3 = -90.0f;
                }
                this.mRot = n3 * n;
                ViewHelper.setPivotX(view, view.getMeasuredWidth());
                ViewHelper.setPivotY(view, 0.5f * view.getMeasuredHeight());
                ViewHelper.setRotationY(view, this.mRot);
            }
            if (view2 != null) {
                this.manageLayer(view2, true);
                if (!b) {
                    n2 = -90.0f;
                }
                this.mRot = -n2 * (1.0f - n);
                ViewHelper.setPivotX(view2, 0.0f);
                ViewHelper.setPivotY(view2, 0.5f * view2.getMeasuredHeight());
                ViewHelper.setRotationY(view2, this.mRot);
            }
        }
    }

    @TargetApi(11)
    private void disableHardwareLayer() {
        if (JazzyViewPager.API_11) {
            for (int i = 0; i < this.getChildCount(); ++i) {
                final View child = this.getChildAt(i);
                if (child.getLayerType() != 0) {
                    child.setLayerType(0, (Paint)null);
                }
            }
        }
    }

    private boolean isSmall(final float n) {
        return Math.abs(n) < 1.0E-4;
    }

    @TargetApi(11)
    private void manageLayer(final View view, final boolean b) {
        if (JazzyViewPager.API_11) {
            int n;
            if (b) {
                n = 2;
            }
            else {
                n = 0;
            }
            if (n != view.getLayerType()) {
                view.setLayerType(n, (Paint)null);
            }
        }
    }

    private View wrapChild(final View view) {
        if (!this.mOutlineEnabled || view instanceof OutlineContainer) {
            return view;
        }
        final OutlineContainer outlineContainer = new OutlineContainer(this.getContext());
        outlineContainer.setLayoutParams(this.generateDefaultLayoutParams());
        view.setLayoutParams((ViewGroup.LayoutParams)new FrameLayout.LayoutParams(-1, -1));
        outlineContainer.addView(view);
        return (View)outlineContainer;
    }

    private void wrapWithOutlines() {
        for (int i = 0; i < this.getChildCount(); ++i) {
            final View child = this.getChildAt(i);
            if (!(child instanceof OutlineContainer)) {
                this.removeView(child);
                super.addView(this.wrapChild(child), i);
            }
        }
    }

    public void addView(final View view) {
        super.addView(this.wrapChild(view));
    }

    public void addView(final View view, final int n) {
        super.addView(this.wrapChild(view), n);
    }

    public void addView(final View view, final int n, final int n2) {
        super.addView(this.wrapChild(view), n, n2);
    }

    public void addView(final View view, final int n, final LayoutParams layoutParams) {
        super.addView(this.wrapChild(view), n, layoutParams);
    }

    public void addView(final View view, final LayoutParams layoutParams) {
        super.addView(this.wrapChild(view), (ViewGroup.LayoutParams)layoutParams);
    }

    protected void animateFade(final View view, final View view2, final float n) {
        if (view != null) {
            ViewHelper.setAlpha(view, 1.0f - n);
        }
        if (view2 != null) {
            ViewHelper.setAlpha(view2, n);
        }
    }

    protected void animateOutline(final View view, final View view2) {
        if (view instanceof OutlineContainer) {
            if (this.mState != State.IDLE) {
                if (view != null) {
                    this.manageLayer(view, true);
                    ((OutlineContainer)view).setOutlineAlpha(1.0f);
                }
                if (view2 != null) {
                    this.manageLayer(view2, true);
                    ((OutlineContainer)view2).setOutlineAlpha(1.0f);
                }
            }
            else {
                if (view != null) {
                    ((OutlineContainer)view).start();
                }
                if (view2 != null) {
                    ((OutlineContainer)view2).start();
                }
            }
        }
    }

    protected void animateScroll(final int n, final float n2) {
        if (this.mState != State.IDLE) {
            this.mRot = 30.0f * ((float)(1.0 - Math.cos(6.283185307179586 * n2)) / 2.0f);
            float mRot;
            if (this.mState == State.GOING_RIGHT) {
                mRot = this.mRot;
            }
            else {
                mRot = -this.mRot;
            }
            ViewHelper.setRotationY((View)this, mRot);
            ViewHelper.setPivotX((View)this, 0.5f * this.getMeasuredWidth());
            ViewHelper.setPivotY((View)this, 0.5f * this.getMeasuredHeight());
        }
    }

    protected void animateStack(final View view, final View view2, final float n, final int n2) {
        if (this.mState != State.IDLE) {
            if (view2 != null) {
                this.manageLayer(view2, true);
                this.mScale = 0.5f + 0.5f * n;
                this.mTrans = n2 + (-this.getWidth() - this.getPageMargin());
                ViewHelper.setScaleX(view2, this.mScale);
                ViewHelper.setScaleY(view2, this.mScale);
                ViewHelper.setTranslationX(view2, this.mTrans);
            }
            if (view != null) {
                view.bringToFront();
            }
        }
    }

    protected void animateTablet(final View view, final View view2, final float n) {
        if (this.mState != State.IDLE) {
            if (view != null) {
                this.manageLayer(view, true);
                this.mRot = 30.0f * n;
                this.mTrans = this.getOffsetXForRotation(this.mRot, view.getMeasuredWidth(), view.getMeasuredHeight());
                ViewHelper.setPivotX(view, view.getMeasuredWidth() / 2);
                ViewHelper.setPivotY(view, view.getMeasuredHeight() / 2);
                ViewHelper.setTranslationX(view, this.mTrans);
                ViewHelper.setRotationY(view, this.mRot);
            }
            if (view2 != null) {
                this.manageLayer(view2, true);
                this.mRot = -30.0f * (1.0f - n);
                this.mTrans = this.getOffsetXForRotation(this.mRot, view2.getMeasuredWidth(), view2.getMeasuredHeight());
                ViewHelper.setPivotX(view2, 0.5f * view2.getMeasuredWidth());
                ViewHelper.setPivotY(view2, 0.5f * view2.getMeasuredHeight());
                ViewHelper.setTranslationX(view2, this.mTrans);
                ViewHelper.setRotationY(view2, this.mRot);
            }
        }
    }

    public View findViewFromObject(final int n) {
        final Object value = this.mObjs.get(n);
        if (value != null) {
            final PagerAdapter adapter = this.getAdapter();
            for (int i = 0; i < this.getChildCount(); ++i) {
                final View child = this.getChildAt(i);
                if (adapter.isViewFromObject(child, value)) {
                    return child;
                }
            }
            return null;
        }
        return null;
    }

    public boolean getFadeEnabled() {
        return this.mFadeEnabled;
    }

    protected float getOffsetXForRotation(final float n, final int n2, final int n3) {
        this.mMatrix.reset();
        this.mCamera.save();
        this.mCamera.rotateY(Math.abs(n));
        this.mCamera.getMatrix(this.mMatrix);
        this.mCamera.restore();
        this.mMatrix.preTranslate(0.5f * -n2, 0.5f * -n3);
        this.mMatrix.postTranslate(0.5f * n2, 0.5f * n3);
        this.mTempFloat2[0] = n2;
        this.mTempFloat2[1] = n3;
        this.mMatrix.mapPoints(this.mTempFloat2);
        final float n4 = n2 - this.mTempFloat2[0];
        float n5;
        if (n > 0.0f) {
            n5 = 1.0f;
        }
        else {
            n5 = -1.0f;
        }
        return n5 * n4;
    }

    @Override
    public boolean onInterceptTouchEvent(final MotionEvent motionEvent) {
        return this.mEnabled && super.onInterceptTouchEvent(motionEvent);
    }

    public void onPageScrolled(final int n, final float n2, final int n3) {
        if (this.mState == State.IDLE && n2 > 0.0f) {
            this.oldPage = this.getCurrentItem();
            State mState;
            if (n == this.oldPage) {
                mState = State.GOING_RIGHT;
            }
            else {
                mState = State.GOING_LEFT;
            }
            this.mState = mState;
        }
        boolean b;
        if (n == this.oldPage) {
            b = true;
        }
        else {
            b = false;
        }
        if (this.mState == State.GOING_RIGHT && !b) {
            this.mState = State.GOING_LEFT;
        }
        else if (this.mState == State.GOING_LEFT && b) {
            this.mState = State.GOING_RIGHT;
        }
        float n4;
        if (this.isSmall(n2)) {
            n4 = 0.0f;
        }
        else {
            n4 = n2;
        }
        this.mLeft = this.findViewFromObject(n);
        this.mRight = this.findViewFromObject(n + 1);
        this.animateCube(this.mLeft, this.mRight, n4, false);
        super.onPageScrolled(n, n2, n3);
        if (n4 == 0.0f) {
            this.disableHardwareLayer();
            this.mState = State.IDLE;
        }
    }

    public void setFadeEnabled(final boolean mFadeEnabled) {
        this.mFadeEnabled = mFadeEnabled;
    }

    public void setObjectForPosition(final Object o, final int n) {
        this.mObjs.put(n, o);
    }

    public void setOutlineColor(final int sOutlineColor) {
        JazzyViewPager.sOutlineColor = sOutlineColor;
    }

    public void setOutlineEnabled(final boolean mOutlineEnabled) {
        this.mOutlineEnabled = mOutlineEnabled;
        this.wrapWithOutlines();
    }

    public void setPagingEnabled(final boolean mEnabled) {
        this.mEnabled = mEnabled;
    }

    public void startRoll() {
        this.handler.postDelayed((Runnable)this.runnable, 4000L);
    }

    private enum State
    {
        GOING_LEFT("GOING_LEFT", 1),
        GOING_RIGHT("GOING_RIGHT", 2),
        IDLE("IDLE", 0);

        private State(final String s, final int n) {
        }
    }

    class TaskRunnable implements Runnable
    {
        @Override
        public void run() {
            JazzyViewPager.this.currentPostion = (1 + JazzyViewPager.this.currentPostion) % 10;
            JazzyViewPager.this.handler.obtainMessage().sendToTarget();
        }
    }
}
